package org.hxyjs.dao.impl;

import org.hxyjs.dao.InformationDao;
import org.hxyjs.entity.Information;
import org.hxyjs.mapper.InformationMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Repository("informationDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class InformationDaoImpl implements InformationDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private InformationMapper informationMapper;

    //查询全部
    public List<Information> getAllInformations() throws Exception {
        return informationMapper.selectByExample(null);
    }

    //按主键查询
    @Override
    public Information getInformationByIno(String ino) throws Exception {
        return informationMapper.selectByPrimaryKey(ino);
    }

    //分页查询
    @Override
    public List<Information> getInformationByPage(PageInfo pageInfo) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("currPage", pageInfo.getCurrPage());
        map.put("pageSize", pageInfo.getPageSize());
        int itemCount = (int) informationMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return informationMapper.getInformationByPage(map);
    }


    //删除
    @Override
    public Integer deleteInformationByIno(String ino) {
        return informationMapper.deleteByPrimaryKey(ino);
    }

    //修改
    @Override
    public Integer updateInformationByIno(Information information) {
        return informationMapper.updateByPrimaryKey(information);
}


    //增加
    @Override
    public Integer addInformation(Information information) {
        information.setIno(initId());
        return informationMapper.insertSelective(information);
    }

    public String initId() {
        String maxIno = informationMapper.getMaxIno();
        maxIno = Optional.ofNullable(maxIno).orElse("0000");
        return GenNumTools.initId("e", 4, maxIno);
    }


}